<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>年度计划管理</title>
<meta name="decorator" content="default"/>
<link href="${ctxStatic}/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet" />
<link href="${ctxStatic}/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<script src="${ctxStatic}/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="${ctxStatic}/bootstrap-table/bootstrap-table.js"></script>
<script src="${ctxStatic}/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="${ctxStatic}/bootstrap-table/extensions/editable/bootstrap-table-editable.js"></script>
<script src="${ctxStatic}/common/format/formatOrderData.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#tab').bootstrapTable({
		url: "${ctx}/plan/bsPlanYearDetail/formEdit",
    	//toolbar: "#toolbar",
		queryParams: queryParams,//传递参数（*）
    	clickToSelect :true,
    	uniqueId: "id",
    	columns: [{
            field: 'id',
            title: 'id',
            visible: false
        },{
            field: 'configId',
            title: 'configId',
            visible: false
        },{
            field: 'vaccineid',
            title: '疫苗小类',
            visible: false
        },{
            field: 'vaccname',
            title: '疫苗名称'
        },{
            field: 'type',
            formatter : formatSort,
            title: '类型'
        },{
            field: 'spec',
            title: '规格'
        },{
            field: 'dosage',
            title: '免疫程序剂次数'
        },{
            field: 'lossFactor',
            title: '损耗系数'
        },{
			field: 'packNum',
			title: '最小分包装数',
			width: '150px',
			editable: {
				type: 'text',
				emptytext: '0',
				mode: 'inline',
				inputclass: 'input-mini',
				validate: function(v) {
					var sellprice_reg = /^\d+(\.\d+)?$/;
					if (!sellprice_reg.test(v)) {
						return '输入不合法';
					}
				}
			}
		},{
            field: 'monthNum',
            title: '上报月数',
            width:'150px',
            editable: {
                type: 'text',
                defaultValue :'0',
                mode:'inline',
                inputclass:'input-mini',
                validate: function (v) {
    				var sellprice_reg = /^\d+(\.\d+)?$/;
    				if (!sellprice_reg.test(v)) {
    					return '输入不合法';
    				}
                }
            }
        },{
        	field: 'planPopulation',
            title: '计划应种人口数',
            width:'150px',
            editable: {
                type: 'text',
                defaultValue :'0',
                mode:'inline',
                inputclass:'input-mini',
                validate: function (v) {
    				var sellprice_reg = /^\d+(\.\d+)?$/;
    				if (!sellprice_reg.test(v)) {
    					return '输入不合法';
    				}
                }
            }
        },{
        	field: 'planNumYear',
            title: '年度计划数',
            width:'150px',
            editable: {
                type: 'text',
                defaultValue :'0',
                mode:'inline',
                inputclass:'input-mini',
                validate: function (v) {
    				var sellprice_reg = /^\d+(\.\d+)?$/;
    				if (!sellprice_reg.test(v)) {
    					return '输入不合法';
    				}
                }
            }
        },{
        	field: 'planNumMonth',
            title: '月度计划数'
        },{
        	field: 'planNumYearRemain',
            title: '年度累计领取数'
        },{
			field: 'remarks',
			title: '备注',
			width: '150px',
			editable: {
				type: 'text',
				emptytext: '--',
				mode: 'inline',
				inputclass: 'input-mini',
				validate: function(v) {
					
				}
			}
		}],
        onEditableSave: function (field, row, oldValue, $el) {
        	debugger;
        	if (field =='planPopulation'){
        		row.planNumYear = Math.ceil( (row.planPopulation* row.dosage * row.lossFactor) /row.spec );
        		row.planNumMonth = Math.ceil( (row.planNumYear /row.monthNum) * 1.5 );
        		$('#tab').bootstrapTable('updateByUniqueId', { id: row.id, 'row':row});
        	}
        	if (field == 'monthNum' || field == 'planNumYear' ){
        		row.planNumMonth = Math.ceil( (row.planNumYear /row.monthNum) * 1.5 );
        		$('#tab').bootstrapTable('updateByUniqueId', { id: row.id, 'row':row});
        	}
            setTimeout(function() {
            	$('#tab').find('[data-uniqueid="'+row.id+'"]').next().find('[data-name="'+field+'"]').editable('show');
            }, 200);
           }
    });
	
	$("#inputForm").validate({
		submitHandler: function(form){
			postOrderDetil(form);
		},
		errorContainer: "#messageBox",
		errorPlacement: function(error, element) {
			$("#messageBox").text("输入有误，请先更正。");
			if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
				error.appendTo(element.parent().parent());
			} else {
				error.insertAfter(element);
			}
		}
	});
	
	$('#planTitleStr').editable({validate: function(value) {
	    // TODO 校验数字是否合法
	},success: function(response, newValue) {// 成功后回调
		$("#planTitle").val(newValue);
	}});
	
	$("#btnSubmitReport").click(function(){//保存并上报
		var loadIndex = layer.load(2);
		var param = {
			"fromOrgCode" : $("#fromOrgCode").val(),
			"planDate" : $("#planDateStr").val(),
			"toOrgCode" : $("#toOrgCodeId").val()
		};
		$.post("${ctx}/plan/bsPlanYear/checkPlanYear", param, function(result){
			layer.close(loadIndex);
			if (result.success == false){
				layer.alert(result.msg, {icon: 2});
			} else if(result.success == true){
				$("#planStatus").val("1");
				$("#planDate").val($("#planDateStr").val());
				$("#inputForm").submit();
			}
		});
	});
	
	$("#btnSubmit").click(function(){//保存
		$("#planStatus").val("0");
		$("#inputForm").submit();
	});
});

/* function titleChange(){
	var date = $dp.cal.getDateStr("yyyy");
	var fromOrgName = $("#fromOrgName").val();
	var planTitleStr = date+"年度计划-"+fromOrgName;
	$("#planTitle").val(planTitleStr);
	$('#planTitleStr').editable('setValue', planTitleStr, false);
// 	$('#tab').bootstrapTable('refresh');
} */

function queryParams(params) {
    var temp = {
//         planDate : $("#planDate").val(),
        planId: $("#id").val()
    };
    return temp;
};

function postOrderDetil(form) { //表单保存时  将此方法中的数据尽行保存
	var rows = $('#tab').bootstrapTable('getData');
	var result = false;var checkPrice = true;var checkSort = true;
	$("#planDate").val($("#planDateStr").val());
	$("#tableData").val(JSON.stringify(rows));
	layer.confirm('确定保存？', {
		icon: 3, title:'提示',
		btn : [ '保存', '取消' ]
	}, function() {
		layer.load(2);
		loading('正在提交，请稍等...');
		form.submit();
	});
}
</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/plan/bsPlanYear/list">年度计划列表</a></li>
		<li class="active"><a href="#">年度计划修改</a></li>
	</ul>
	<form:form id="inputForm" modelAttribute="bsPlanYear" action="${ctx}/plan/bsPlanYear/save" method="post" class="breadcrumb form-search">
		<form:hidden path="id"/>
		<form:hidden path="fromOrgCode" />
		<form:hidden path="type" />
		<form:hidden path="planStatus" />
		<form:hidden path="planTitle" />
		<form:hidden path="planDate" />
		<input id="tableData" name="tableData" type="hidden">
		<sys:message content="${message}"/>	
		<table class="table table-bordered">
			<tr>
				<th>填报单位：</th>
				<td><input readonly type="text" name="fromOrgName" id="fromOrgName" class="input-medium" value="${bsPlanYear.fromOrgName}"/></td>
				<th>填报人：</th>
				<td><input type="text" name="planBy" id="planBy" class="input-medium" value="${bsPlanYear.planBy}"/></td>
				<th>审核单位：</th>
				<td><sys:treeselect2 id="toOrgCode" name="toOrgCode" value="${bsPlanYear.toOrgCode}" labelName="toOrgName" labelValue="${bsPlanYear.toOrgName}"
						title="部门" url="/sys/office/treeData2?areaType=3" cssClass="input-medium" allowClear="true" notAllowSelectParent="true"/></td>
				<th>计划日期：</th>
				<td><input name="planDateStr" id="planDateStr" type="text" readonly="readonly" disabled 
						maxlength="20" class="input-medium Wdate "
						value="<fmt:formatDate value="${bsPlanYear.planDate}" pattern="yyyy"/>"
						onclick="WdatePicker({dateFmt:'yyyy',isShowClear:false,onpicked: titleChange});"/></td>
			</tr>
		</table>
		<div>
			<shiro:hasPermission name="plan:bsPlanYear:edit">
				<button id="btnSubmit" class="btn btn-primary" type="button">保存</button>
				<button id="btnSubmitReport" class="btn btn-primary" type="button" >保存并上报</button>
			</shiro:hasPermission>
			<!-- <button id="btnPrint" class="btn btn-primary" type="button" disabled>打印</button> -->
			<button id="btnCancel" class="btn" type="button" onclick="history.go(-1)">返回</button>
		</div>
	</form:form>
	
	<div class="control-group table">
		<fieldset>
		    <legend>
		    <span id = "planTitleStr" data-mode = "inline"  data-inputclass="input-xlarge" data-type="text">${bsPlanYear.planTitle}</span>
		    </legend>
		</fieldset>
		<table id="tab" class="table table-bordered table-condensed">
		</table>
	</div>
</body>
</html>